//
// Created by Shao Wenhan on 2021/6/23.
//

#include "ConfigurableEventListener.h"
#include "BinaryIndexedTree.h"

class TestBinaryIndexedTree: public testing::Test{
public:
    const int size = 12;
    BinaryIndexedTree<int> binaryIndexedTree;
    TestBinaryIndexedTree(): binaryIndexedTree(size){
        binaryIndexedTree.initialize();
    };
};

TEST_F(TestBinaryIndexedTree, InitialSumFromStartToIndex)
{
    for(int i = 0; i < size; i++)
        ASSERT_EQ(0, binaryIndexedTree.sumFromStartToIndex(i));
}

TEST_F(TestBinaryIndexedTree, SumFromStartToIndexAfterUpdate)
{
    binaryIndexedTree.changeValueOfIndex(5, 3);
    for(int i = 0; i < 5; i++)
        ASSERT_EQ(0, binaryIndexedTree.sumFromStartToIndex(i));
    for(int i = 5; i < size; i++)
        ASSERT_EQ(3, binaryIndexedTree.sumFromStartToIndex(i));
}

TEST_F(TestBinaryIndexedTree, ValueOfIndex)
{
    binaryIndexedTree.changeValueOfIndex(5, 3);

    for(int i = 0; i < size; i++)
    {
        if(i == 5)
            ASSERT_EQ(3, binaryIndexedTree.valueOfIndex(i));
        else
            ASSERT_EQ(0, binaryIndexedTree.valueOfIndex(i));
    }
}

TEST_F(TestBinaryIndexedTree, ScaleAllValues)
{
    binaryIndexedTree.changeValueOfIndex(5, 3);
    binaryIndexedTree.scaleAllValues(3);

    for(int i = 0; i < size; i++)
    {
        if(i == 5)
            ASSERT_EQ(9, binaryIndexedTree.valueOfIndex(i));
        else
            ASSERT_EQ(0, binaryIndexedTree.valueOfIndex(i));
    }

    binaryIndexedTree.scaleAllValues(1.0/9);
    for(int i = 0; i < size; i++)
    {
        if(i == 5)
            ASSERT_EQ(1, binaryIndexedTree.valueOfIndex(i));
        else
            ASSERT_EQ(0, binaryIndexedTree.valueOfIndex(i));
    }
}

GTEST_API_ int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    initialGTest();
    return RUN_ALL_TESTS();
}